package com.agg.persist.ast;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.util.JdbcConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DruidSimpleAst {
    public static void main(String[] args) {
        SQLSelect sqlSelect = new SQLSelect();
        SQLSelectQueryBlock queryBlock = new SQLSelectQueryBlock();
        SQLExpr expr=new SQLIdentifierExpr("t1");
        SQLExprTableSource tableSource=new SQLExprTableSource(expr,"x");
        SQLExpr colExpr=new SQLIdentifierExpr("id");
        queryBlock.addSelectItem(colExpr);
        queryBlock.setFrom(tableSource);
        sqlSelect.setQuery(queryBlock);
        String sql = SQLUtils.toSQLString(sqlSelect);
        String dbType = JdbcConstants.MYSQL;





        SQLBinaryOpExpr opExpr1=  getLikeExpr("product_name","productName","cm","t1");
        SQLBinaryOpExpr opExpr2=  getLikeExpr("category_name","productName","cm","t1");
        SQLBinaryOpExpr opExpr3=  getLikeExpr("desc","productName","cm","t1");




        SQLBinaryOpExpr all1=new SQLBinaryOpExpr();
        all1.setOperator(SQLBinaryOperator.BooleanOr);
        all1.setLeft(opExpr1);
        all1.setRight(opExpr2);

        SQLBinaryOpExpr all2=new SQLBinaryOpExpr();
        all2.setLeft(all1);
        all2.setRight(opExpr3);
        all2.setOperator(SQLBinaryOperator.BooleanOr);

        SQLBinaryOpExpr opExpr4=getEqExpr("id","id","cm","t1");


        SQLBinaryOpExpr all3=new SQLBinaryOpExpr();
        all3.setLeft(all2);
        all3.setRight(opExpr4);
        all3.setOperator(SQLBinaryOperator.BooleanAnd);

        SQLInListExpr  opExpr5=inExpr();

        SQLBinaryOpExpr all4=new SQLBinaryOpExpr();
        all4.setLeft(all3);
        all4.setOperator(SQLBinaryOperator.BooleanAnd);
        all4.setRight(opExpr5);

       // System.out.println(all2.toString());
        sql= SQLUtils.addCondition(sql,
                all4.toString(),
                SQLBinaryOperator.BooleanAnd,
                false,dbType);


        System.out.println(sql);
    }


    private static SQLInListExpr inExpr(){
        SQLBinaryOpExpr opExpr=new SQLBinaryOpExpr();
        SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr("t");
        SQLPropertyExpr left = new SQLPropertyExpr(sqlIdentifierExpr, "id");
        opExpr.setOperator(SQLBinaryOperator.BooleanAnd);
      //  sqlIdentifierExpr.setParent(left);
        opExpr.setLeft(left);

        SQLInListExpr  sqlInListExpr = new SQLInListExpr(left);
        // 构造 in 内包含的东西
        List<SQLExpr> refExprList = new ArrayList<>();
        List<Long> ids= Arrays.asList(1L, 2L, 3L, 4L);
        for (Long id:ids) {
            SQLCharExpr sqlCharExpr = new SQLCharExpr(String.valueOf(id));
           // sqlCharExpr.setParent(left.getParent());
            refExprList.add(sqlCharExpr);
        }
        sqlInListExpr.setTargetList(refExprList);
        opExpr.setRight(sqlInListExpr);
        return sqlInListExpr;
    }
    /**
     * 拼装，like 比较
     * @param column
     * @param owner
     * @return
     */
    private static SQLBinaryOpExpr getLikeExpr(String column,String var, String holer, String owner){
        SQLBinaryOpExpr opExpr=new SQLBinaryOpExpr();
        opExpr.setOperator(SQLBinaryOperator.Like);
        SQLPropertyExpr left=getLeftExpr(column,owner);
        opExpr.setLeft(left);
        SQLIdentifierExpr right= getRightExpr(var,holer);
        opExpr.setRight(right);
        return opExpr;
    }

    /**
     * 拼装左侧变量，例如：p.product_name 的变量
     * @param column
     * @param owner
     * @return
     */
    private static   SQLPropertyExpr getLeftExpr(String column,String owner){
        SQLIdentifierExpr sqlIdentifierExpr = new SQLIdentifierExpr(owner);
        SQLPropertyExpr left = new SQLPropertyExpr(sqlIdentifierExpr, column);
        sqlIdentifierExpr.setParent(left);
        return left;
    }
    /**
     * 拼装，例如：#{cm.name} 的变量
     * @param name
     * @param holer
     * @return
     */
    private  static SQLIdentifierExpr getRightExpr(String name,String holer){
        String paramTpl="#{%s.%s}";
        String paramVar=String.format(paramTpl,holer,name);
        String likeStr="CONCAT('%', "+paramVar+", '%')";
        SQLIdentifierExpr  right = new SQLIdentifierExpr (likeStr);
        return right;
    }

    /**
     * 拼装，eq 比较
     * @param column
     * @param owner
     * @return
     */
    private  static SQLBinaryOpExpr getEqExpr(String column,String var,String holer,String owner){
        SQLBinaryOpExpr opExpr=new SQLBinaryOpExpr();
        opExpr.setOperator(SQLBinaryOperator.Equality);
        SQLPropertyExpr left=getLeftExpr(column,owner);
        opExpr.setLeft(left);
        SQLIdentifierExpr right= getRightExpr(var,holer);
        opExpr.setRight(right);
        return opExpr;
    }


}
